ADC INTERFACING WITH AVR
Microcontroller understands only digital language. However, the inputs available from the environment to the microcontroller are mostly analog in nature, i.e., they vary continuously with time. In order to understand the inputs by the digital processor, a device called Analog to Digital Converter (ADC) is used.
Synopsis

Microcontroller understands only digital language. However, the inputs available from the environment to the microcontroller are mostly analog in nature, i.e., they vary continuously with time. In order to understand the inputs by the digital processor, a device called Analog to Digital Converter (ADC) is used.


For this, a ADC or analog to digital converter is needed. Most modern MCU including AVRs has an ADC on chip.

An ADC converts an input voltage into a number. An ADC has resolution,. A 10 Bit ADC has a range of 0-1023.(2^10=1024) the ADC also has a Reference voltage (aRef). When input voltage is GND the output is 0 and when input voltage is equal to Aref the output is 1023. So the input range is 0-Aref and digital output is 0-1023.


Description

A AVR microcontroller has inbuilt ADC for A/D conversion. The ADC module of ATMEGA16 controller has following specifications:

1. 8 Channels.

2. 10-bit Resolution.

2. 10-bit Resolution.

3. Input voltage range of 0 to Vcc.

4. Selectable 2.56V of internal Reference voltage source.

5. AREF pin for External Reference voltage.

6. ADC Conversion Complete Interrupt.

Inbuilt ADC of AVR

The ADC is multiplexed with PORTA that means the ADC channels are shared with PORTA. The ADC can be operated in single conversion and free running more. In single conversion mode the ADC  does the conversion and then stop.

ADC channels in Atmega16 are multiplexed with PORTA and use the common pins (pin33 to pin40) with PORTA. ADC system  of Atmega16 microcontroller consists of following pins:

i.  ADC0-ADC7: 8 Channels from Pin 40 to Pin 33 of Atmega16 ADC peripheral.

ii. AREF: Pin32 of Atmega16 microcontroller, the voltage on AREF pin acts as the reference voltage for ADC conversion, reference voltage is always less than or equal to the supply voltage, i.e., Vcc.

iii. AVCC: Pin30, this pin is the supply voltage pin for using PORTA and the ADC.

AVCC pin must be connected to Vcc (microcontroller supply voltage) to use PORTA and ADC.

Note:  External reference voltage source can be used at AREF pin. However, Atmega16 also has internal reference voltage options of 2.56V and Vref = Vcc.

ADC Prescaler

The ADC needs a clock pulse to do its conversion. This clock generated by system clock by dividing it to get smaller frequency. The ADC requires a frequency between 50KHz to 2000KHz. At higher frequency the conversion is fast while a lower frequency the conversion is more accurate. First, the system frequency can be set to any value by their user (using internal or externals oscillators).So the Prescaler is provided that produces acceptable frequency for ADC from any system clock frequency. System clock can be divided by 2,4,16,32,64,128 by setting the Prescaler.

ADC Channels

ADC in Atmega16 has 8 channels that means we can take samples from eight different terminal. We can connect up to 8 different sensors and get their values separately.

ADC Channels

ADC in Atmega16 has 8 channels that means we can take samples from eight different terminal. We can connect up to 8 different sensors and get their values separately.

The figure below shows the pin configuration for ADC system of Atmega16 microcontroller.


ADC Registers

ADC has only four registers.

1. ADC Multiplexer selection Register –ADMUX

For selecting the reference voltage and the input channel.

2. ADC Control and Status Register A- ADCSRA

As the name says it has the status of ADC and is also use for controlling it.

3. The ADC Data Register-ADCL and ADCH

The final result of conversion is here.

We have to configure the ADC by setting up ADMUX and ADCSRA registers.

The ADMUX has following bits.

To use the ADC peripheral of Atmega16, certain registers need to be configured.

i.  ADMUX (ADC Multiplexer And Selection Register)


REFS[0:1] bits determine the source of reference voltage whether it is internal or the external voltage source connected to AREF pin. MUX[4:0] bits are used to select between the channels which will provide data to ADC for conversion. ADLAR bit when set to 1 gives the left adjusted result in data registers ADCH and ADCL.

ii.  ADCSRA (ADC Control and Status Register)


ADEN: ADC Enable bit, this bit must be set to 1 for turning ADC on.

ADSC:  ADC Start Conversion bit, this bit is set to 1 to start ADC conversion, as soon as conversion is completed this bit is set back to 0 by the hardware.

ADATE: ADC Auto Trigger Enable, this bit is set to 1 to enable auto triggering of ADC conversion.

ADIF: ADC Interrupt Flag, this bit is set to 1 when ADC conversion gets complete.

ADIE: ADC Interrupt Enable, this bit is set to 1 if we want to activate the ADC conversion complete interrupt.

ADPS[0:2]: ADC Prescaler bits, these bits are used to set the ADC clock frequency, the configuration of these bits determine the division factor by which the microcontroller clock frequency is divided to get the ADC clock frequency. The figure above shows the prescaler bit values for respective division factor.

ADC Clock Frequency = XTAL Frequency Prescaler

Example:      XTAL Freq = 12 MHz

Prescaler = 32

ADC Clock Freq = 12000000/ 32

= 375KHz

The ADC clock frequency must lie somewhere between 50 KHz to 200 KHz.

iii.  ADCH & ADCL (ADC Data Registers) 

When the ADC conversion is complete the data is stored in these two registers. The data configuration depends on the ADLAR bit value of ADMUX register. If ADLAR=0, data is right adjusted and if ADLAR=1, data is left adjusted.  Always read ADCL first and then ADCH. In cases where the 8-bit precision is enough set the ADLAR bit to 1 to left adjust the data and read only the ADCH data register.

When ADLAR = 0,


When ADLAR = 1,


Working with ADC 

To select an analog channel of ATmega’s16 in-built ADC and provide an analog input (0 to 5 volt) to it using a variable resistor or preset (20 k ). 

Programming Steps

To interface analog device with AVR microcontroller, follow the following steps for programming it.

Step1: To initialize ADC

i.Set the value in ADMUX register according to the ADC channel and the reference voltage.

ii.Set the Prescaler bits accordingly in ADCSRA register.

iii.Set the ADEN bit to enable the ADC. 

Step2: To read the analog value

i.Put the channel value in ADMUX

ii.Start the conversion by setting the ADSC bit.

iii.Monitor the ADIF bit for conversion complete.

iv.Clear the conversion bit ADIF. By writing it 1.

v.Digital converted result is now available in ADCH and ADCL registers.

Proteus design for Inbuild ADC of AVR


Orcad design for ADC interfacing with AVR


ADC interfacing with AVR

/*  Name     : main.c
 *  Purpose  : Source code for ADC interface with ATMEGA16.
 *  Author   : Gemicates
 *  Date     : 2017-09-07
 *  Website  : www.gemicates.org
 *  Revision : None
 */
#include<avr/io.h>
#include<util/delay.h>
 
void ADC_init(void);
unsigned int ADC_read();
 
int main(void)
{
	unsigned int value;
	DDRB=0xFF;
	ADC_init();	                                        // Initialization of ADC

	while(1)
	{
		value=ADC_read();
		PORTB=value;
		_delay_ms(500);
	} 
}
 
void ADC_init(void)		                                // Initialization of ADC
{ 
	ADMUX=(1<<REFS0);	                                // AVcc with external capacitor at AREF
	ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);	// Enable ADC and set Prescaler division factor as 128
} 
unsigned int ADC_read()
{	
	ADMUX |=0x00;		                                // selecting channel
 	ADCSRA|=(1<<ADSC);	                                // start conversion
	while(ADCSRA==0x9f);                                    // waiting for ADIF, conversion complete
	ADCSRA|=(1<<ADIF);	                                // clearing of ADIF, it is done by writing 1 to it
 	return (ADC);
}

Error message here!

Show Error message here!


Forgot your password?

Error message here!

Send OTP

Error message here!

Show Error message here!


Lost your password? Please enter your email address. You will receive a password you Need.

Send Error message here!


Back to log-in

Close